import { http } from '@/api';
import { PORT1 } from '@/api/config/port';
import {
  Action,
  Owner,
  Permission,
  PermissionSet,
  Role,
  User,
  UserSearch,
  RoleAddUserReq,
  OwnerReq,
} from '../interface/authentication';
import { ReqPage, ResPage } from '../interface';

// 获取历史操作记录
export const ActionList = (params: ReqPage & { q: string }) => {
  return http.post<ResPage<Action>>(PORT1 + `/admin/auth/action/list`, params);
};

// 角色列表
export const RoleList = () => {
  return http.post<Role[]>(PORT1 + `/admin/auth/role/current`, {});
};

// 添加角色
export const RoleAdd = (params: Role) => {
  return http.post(PORT1 + `/admin/auth/role/add`, params);
};

// 编辑角色
export const RoleEdit = (params: Role) => {
  return http.post(PORT1 + `/admin/auth/role/update`, params);
};

// 删除角色
export const RoleDel = (params: { id: number }) => {
  return http.post(PORT1 + `/admin/auth/role/delete`, {}, { params });
};

// 给角色删除用户
export const RoleDelUser = (params: { roleId: number; userId: number }) => {
  return http.post(PORT1 + `/admin/auth/user/role/delete`, {}, { params });
};

// 给角色添加用户
export const RoleAddUser = (params: RoleAddUserReq) => {
  return http.post(PORT1 + `/admin/auth/user/role/add`, params);
};

// 根据角色 id 查询用户列表
export const UserListByRoleId = (params: UserSearch) => {
  return http.post<ResPage<User>>(PORT1 + `/admin/auth/user/list`, params);
};

// 用户列表不分页，用于添加用户角色
export const UserListAll = (params?: { name: string; orgId?: number }) => {
  return http.post<User[]>(PORT1 + `/admin/auth/user/find`, params);
};

// 根据角色 id 获取菜单 permission
export const PermissionByRoleId = (params: { roleId: number }) => {
  return http.post<Permission>(
    PORT1 + `/admin/auth/permission/list`,
    {},
    { params }
  );
};

// 给角色 id 设置菜单 permission
export const PermissionSetByRoleId = (params: PermissionSet) => {
  return http.post(PORT1 + `/admin/auth/role/permission/update`, params);
};

// 用户列表
export const UserList = (params: UserSearch) => {
  return http.post<ResPage<User>>(PORT1 + `/admin/auth/user/all`, params);
};

// 获取操作用户的角色
export const RoleListByUserId = (params: { userId: string }) => {
  return http.post<User>(PORT1 + `/admin/auth/user/role/get`, {}, { params });
};

// 用户设置角色
export const UserSetRole = (params: { userId: number; roleId: number[] }) => {
  return http.post(PORT1 + `/admin/auth/user/role/update`, params);
};

// 获取 Owner 机构列表
export const OwnerList = () => {
  return http.get<Owner[]>(PORT1 + `/admin/auth/owner/list`);
};

// 查询机构下的用户, 用于设置超级管理员
export const OwnerUserList = (params: { orgId: number }) => {
  return http.get<User[]>(PORT1 + `/admin/auth/owner/candidate`, params);
};

// 给机构设置 Owner 用户
export const UpdateOwner = (params: OwnerReq) => {
  return http.post(PORT1 + `/admin/auth/owner/update`, params);
};
